var zTree;

var setting = {  

 view : {
    showLine:false,
    expandSpeed : "",
    
    selectedMulti : false
},
data: {  
    simpleData: {  
        enable: true  
    }  
},  
check: {  
    enable: false  
},  
callback: {  
   
   onClick:zTreeOnClick,
   
   onRightClick: OnRightClick  
}
};  





var  rMenu;
                   //加载ztree  
                   function onloadZTree(url,title){  
                       var ztreeNodes = getZnodes(url,title); 
                       $.fn.zTree.init($( "#treeDemo"), setting, ztreeNodes);  
                       zTree = $.fn.zTree.getZTreeObj("treeDemo");  
                       rMenu = $("#rMenu");  

                   }


                   function getZnodes(url,title){


                    var datas =  [];
                    $.ajax( {  
	                async : false, //是否异步  
	                cache : false, //是否使用缓存  
	                type : 'get', //请求方式,post  
	                dataType : "json", //数据传输格式  
	                url : url, //请求链接  
	                error : function() {  
                       console.log('亲，网络有点不给力呀！');  
                   },  
                   success : function(data) { 
                      var temps =  [];
                      temps.push({id:0, pId:-1, name:title, t:"",open:true });
                      for(var i=0;i<data.length;i++){
                          var obj =  { id: data[i].Id, pId: data[i].FatherId, name: data[i].ClassName, t: data[i].ClassLongName, open: false,noRemoveBtn:true,noEditBtn:true }
                          temps.push(obj);
                      }
                      datas = temps;
                      
                  }  
              });  
                    return datas;

                }



                function zTreeOnClick(event, treeId, treeNode){
                   zTreeOnClickDetail(event, treeId, treeNode);
                   $("#operation").val(treeNode.id);
               }






// 在ztree上的右击事件  
function OnRightClick(event, treeId, treeNode) {  
    if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {  
        showRMenu("root", event.clientX, event.clientY);  
    } else if (treeNode && !treeNode.noR) {  
        showRMenu("node", event.clientX, event.clientY);  
    }  
    $("#operation").val(treeNode.id);
}  

//显示右键菜单  
function showRMenu(type, x, y) {  
    $("#rMenu ul").show();  
    rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"}); //设置右键菜单的位置、可见  
    $("body").bind("mousedown", onBodyMouseDown);  
}  
//隐藏右键菜单  
function hideRMenu() {  
    if (rMenu) rMenu.css({"visibility": "hidden"}); //设置右键菜单不可见  
    $("body").unbind("mousedown", onBodyMouseDown);  
}  
//鼠标按下事件  
function onBodyMouseDown(event){  
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {  
        rMenu.css({"visibility" : "hidden"});  
    }  
}  





